﻿var MvcCmsImages = function(basePath, width, height, selectFunction) {
}
MvcCmsImages.prototype = {

    // Renders MvcCmsImages in the current document.
    Create: function() {
        document.write(this.CreateHtml());
    },

    // Gets the HTML needed to create a MvcCmsImages instance.
    CreateHtml: function() {
        var className = this.ClassName;
        if (className && className.length > 0)
            className = ' class="' + className + '"';

        var id = this.Id;
        if (id && id.length > 0)
            id = ' id="' + id + '"';

        return '<iframe src="' + this._BuildUrl() + '" width="' + this.Width + '" ' +
			'height="' + this.Height + '"' + className + id + ' frameborder="0" scrolling="no"></iframe>';
    },

    // Opens MvcCmsImages in a popup. The "width" and "height" parameters accept
    // numbers (pixels) or percent (of screen size) values.
    Popup: function(width, height) {
        width = width || '80%';
        height = height || '70%';

        if (typeof width == 'string' && width.length > 1 && width.substr(width.length - 1, 1) == '%')
            width = parseInt(window.screen.width * parseInt(width) / 100);

        if (typeof height == 'string' && height.length > 1 && height.substr(height.length - 1, 1) == '%')
            height = parseInt(window.screen.height * parseInt(height) / 100);

        if (width < 200)
            width = 200;

        if (height < 200)
            height = 200;

        var top = parseInt((window.screen.height - height) / 2);
        var left = parseInt((window.screen.width - width) / 2);

        var options = 'location=no,menubar=no,toolbar=no,dependent=yes,minimizable=no,modal=yes,alwaysRaised=yes,resizable=yes' +
			',width=' + width +
			',height=' + height +
			',top=' + top +
			',left=' + left;

        var popupWindow = window.open('', 'MvcCmsImagesPopup', options, true);

        // Blocked by a popup blocker.
        if (!popupWindow)
            return false;

        var url = this._BuildUrl().replace(/&amp;/g, '&');
        try {
            popupWindow.moveTo(left, top);
            popupWindow.resizeTo(width, height);
            popupWindow.focus();
            popupWindow.location.href = url;
        }
        catch (e) {
            popupWindow = window.open(url, 'MvcCmsImagesPopup', options, true);
        }

        return true;
    }

};


MvcCmsImages.Create = function(basePath, width, height, selectFunction) {
    var mvccmsimages = new MvcCmsImages(basePath, width, height, selectFunction);

    MvcCmsImages.Create();
}

MvcCmsImages.Popup = function(basePath, width, height, selectFunction) {
    var mvccmsimages = new MvcCmsImages(basePath, width, height, selectFunction);

    MvcCmsImages.Popup(width, height);
}


MvcCmsImages.SetupCKEditor = function(editorObj) {
    if (editorObj === null) {
        // Setup current instances
        for (var editorName in CKEDITOR.instances)
            MvcCmsImages.SetupCKEditor(CKEDITOR.instances[editorName], basePath, imageType, flashType);

        // Future instances
        CKEDITOR.on('instanceCreated', function(e) {
            MvcCmsImages.SetupCKEditor(e.editor, basePath, imageType, flashType);
        });
        return;
    }

    editorObj.config.filebrowserImageBrowseUrl = "/backoffice/imagemanager";
}
